import {
  request
} from "../../request/index"

// pages/goods_detail/goods_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail: null,
    swiperList: [],
    vNode: "",
    goods_price: 0,
    goods_name: "",
    goods_id: 0,
    icon: [{
      name: 'favorfill',
      isShow: true,
      value: "已收藏"
    }, {
      name: 'favor',
      isShow: true,
      value: "收藏"
    }],
    activeIndex: 1,
    cartCount: 0,
    cartList: [],
   
  },
  previewImg(event) {
    const {
      pics_big
    } = event.currentTarget.dataset;
    wx.previewImage({
      current: pics_big, // 当前显示图片的http链接
      urls: this.data.swiperList.map(item => item.pics_big) // 需要预览的图片http链接列表
    })
  },
  toggleFavor() {
    let favor_list = wx.getStorageSync('hy1_favor_list') || [];
    const flag = favor_list.findIndex(item => item.goods_id == this.data.goods_id);
    if (flag == -1) {
      favor_list.push(this.data.goodsDetail);
    } else {
      favor_list.splice(flag, 1);
    }
    wx.setStorageSync('hy1_favor_list', favor_list);
    this.favorIsActive(); //切换收藏或已收藏显示
    console.log('收藏夹', favor_list);
  },
  async getSwiperList(id) {
    const data = await request({
      url: "/goods/detail?goods_id=" + id
    })
    const {
      pics,
      goods_introduce,
      goods_price,
      goods_name,
      goods_id
    } = data;
    this.setData({
      goodsDetail: data,
      swiperList: pics,
      vNode: goods_introduce,
      goods_price,
      goods_name,
      goods_id,
    })
    this.favorIsActive();
    this.showCartCount();
    console.log(data);
  },
  favorIsActive() {
    let favor_list = wx.getStorageSync('hy1_favor_list') || [];
    console.log(favor_list);
    const index = favor_list.findIndex(item => item.goods_id == this.data.goods_id);
    if (index !== -1) {
      this.data.icon
      this.setData({
        activeIndex: 0
      })
    } else {
      this.setData({
        activeIndex: 1
      })
    }
  },
  addCart() {
    let cartList = wx.getStorageSync('hy1_cart_list') || [];
    const currentGoods = cartList.find(item => item.goods_id === this.data.goods_id);
    if (currentGoods) {
      currentGoods.count++;
    } else {
      cartList.push({
        ...this.data.goodsDetail,
        count: 1,
        isChecked: false
      });
    }

    wx.setStorageSync('hy1_cart_list', cartList);
    wx.showToast({
      title: '添加成功',
    })
    this.showCartCount();
    console.log('购物车列表', cartList);
  },
  showCartCount() {
    let cartList = wx.getStorageSync('hy1_cart_list') || [];
    console.log(cartList);
    this.setData({
      cartCount: cartList.length,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      goods_id
    } = options
    this.getSwiperList(goods_id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})